//
//  PhotoCell.m
//  DingDing
//
//  Created by LiynXu on 2016/10/27.
//  Copyright © 2016年 LiynXu. All rights reserved.
//

#import "PhotoCell.h"

@interface PhotoCell()
@property (nonatomic,strong) UIImageView *photo;
@property (nonatomic,strong) UIView *coverView;
@property (nonatomic,strong) UIImageView *chooseView;
@property (nonatomic,assign) float scale;
@end

@implementation PhotoCell
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self customViews];
    }
    return  self;
}
- (void)customViews{
    _photo = [[UIImageView alloc] initWithFrame:CGRectMake(1, 1, ItemWidth()-2, ItemHieght()-2)];
    _photo.userInteractionEnabled = YES;
    _photo.layer.borderWidth = 1.0;
    _photo.contentMode = UIViewContentModeScaleAspectFit;
    //UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(choose)];
    //[_photo addGestureRecognizer:tap];
    [self.contentView addSubview:_photo];
    _coverView = [[UIView alloc] initWithFrame:CGRectMake(1, 1, ItemWidth()-2, ItemHieght()-2)];
    _coverView.backgroundColor = [UIColor colorWithRed:120/255.0 green:226/255.0 blue:238/255.0 alpha:0.8];
    _coverView.alpha = 0;
    [self.photo addSubview:_coverView];
    _chooseView = [[UIImageView alloc] initWithFrame:CGRectMake(ItemWidth()-30, ItemHieght()-30, 20, 20)];
    _chooseView.image = [UIImage imageNamed:@"noChoose"];
    [self.contentView addSubview:_chooseView];
}
-(void)setPhotoAsset:(PHAsset *)photoAsset{
    _photoAsset = photoAsset;
    WS(weakSelf)
    [PhotoManager requestImageForAsset:_photoAsset
                         resultHandler:^(UIImage *image) {
                             weakSelf.photo.image = image;
                         }];
}
-(void)setPhotoImage:(UIImage *)photoImage{
    _photoImage = photoImage;
    [self refreshCell];
}
- (void)setIsChoose:(BOOL)isChoose{
    _isChoose = isChoose;
    _photo.layer.borderColor = _isChoose?[[UIColor greenColor] CGColor]:[[UIColor clearColor] CGColor];
    NSString *string = _isChoose?@"choosed":@"noChoose";
    _chooseView.image = [UIImage imageNamed:string];
    _coverView.alpha = _isChoose/3.0;
}

- (void)refreshCell{
    _photo.image = _photoImage;
    NSLog(@"%@ %@",_photo.image,_photoImage);
}
-(void)layoutSubviews{
    _photo.frame = CGRectMake(1, 1, ItemWidth()-2, ItemHieght()-2);
    _coverView.frame = CGRectMake(1, 1, ItemWidth()-2, ItemHieght()-2);
}

@end
